package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.adpater.impl.JDAggrateAdapter;
import com.xyy.saas.payment.adpater.jd.service.JDAdpaterService;
import com.xyy.saas.payment.adpater.jd.service.JDAggregateAdpaterService;
import com.xyy.saas.payment.adpater.jd.vo.*;
import com.xyy.saas.payment.adpater.jd.vo.aggrate.*;
import com.xyy.saas.payment.callback.core.job.SyncRefundStatusJob;
import com.xyy.saas.payment.cores.api.PaymentOpenAccountApi;
import com.xyy.saas.payment.cores.bo.QueryResultBo;
import com.xyy.saas.payment.cores.bo.jd.*;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.cores.vo.jd.*;
import com.xyy.saas.payment.dao.model.RefundOrder;
import com.xyy.saas.payment.merchant.core.dto.QueryTransactionStatusDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = {"/api/jdpay"})
public class JDCardPayTestController {
    @Autowired
    private JDAdpaterService jdAdpaterService;
    @Autowired
    private JDAggregateAdpaterService jdAggregateAdpaterService;
    @Resource
    private PaymentOpenAccountApi paymentOpenAccountApi;
    @Autowired
    private JDAggrateAdapter jdAggrateAdapter;
    @Autowired
    private SyncRefundStatusJob syncRefundStatusJob;

    /**
     * 京东聚合支付 - 聚合下单
     */
    @PostMapping(value = "/aggregateCreateOrder", produces = "application/json;charset=UTF-8")
    public ResultVO<JdPayAggregateCreateOrderResponse> aggregateCreateOrder(@RequestBody JdPayAggregateCreateOrderVo param) throws Exception {
        return ResultVO.createSuccess(jdAggregateAdpaterService.aggregateCreateOrder(param.getOutTradeNo(), param));
    }

    /**
     * 京东聚合支付 - 退款
     */
    @PostMapping(value = "/aggrateRefund", produces = "application/json;charset=UTF-8")
    public ResultVO<JdPayAggregateRefundResponse> aggrateRefund(@RequestBody JdPayAggregateRefundVo param) throws Exception {
        return ResultVO.createSuccess(jdAggregateAdpaterService.aggrateRefund(param.getOutTradeNo(), param));
    }

    /**
     * 京东聚合支付 - 支付查询
     */
    @PostMapping(value = "/queryAggrateOrder", produces = "application/json;charset=UTF-8")
    public ResultVO<JdPayAggregateQueryOrderResponse> queryAggrateOrder(@RequestBody JdPayAggregateQueryOrderVo param) throws Exception {
        return ResultVO.createSuccess(jdAggregateAdpaterService.queryAggrateOrder(param.getOutTradeNo(), param));
    }

    /**
     * 京东聚合支付 - 退款查询
     */
    @PostMapping(value = "/queryAggrateRefund", produces = "application/json;charset=UTF-8")
    public ResultVO<JdPayAggregateRefundQueryResponse> queryAggrateRefund(@RequestBody JdPayAggregateRefundQueryVo param) throws Exception {
        return ResultVO.createSuccess(jdAggregateAdpaterService.queryAggrateRefund(param.getOutTradeNo(), param));
    }

    /**
     * 京东聚合支付 - 关单
     */
    @PostMapping(value = "/aggrateClose", produces = "application/json;charset=UTF-8")
    public ResultVO<JdPayAggregateCloseResponse> aggrateClose(@RequestBody JdPayAggregateCloseVo param) throws Exception {
        return ResultVO.createSuccess(jdAggregateAdpaterService.aggrateClose(param.getOrignalOutTradeNo(), param));
    }

    /**
     * 京东聚合支付 - 查询支付结果
     */
    @PostMapping(value = "/queryAggregate", produces = "application/json;charset=UTF-8")
    public ResultVO<QueryResultBo> queryAggregate(@RequestBody QueryTransactionStatusDto param) throws Exception {
        QueryResultBo queryResultBo = jdAggrateAdapter.queryTransactionStatus(param);
        return ResultVO.createSuccess(queryResultBo);
    }

    /**
     * 京东一键签约 - 查询一键签约列表
     */
    @PostMapping(value = "/queryOneKeySignBanks", produces = "application/json;charset=UTF-8")
    public ResultVO<List<JDBankDto>> queryOneKeySignBanks(@RequestBody JDQueryOneKeySignBanksVo jdQueryOneKeySignBanksVo){
        return paymentOpenAccountApi.queryOneKeySignBanks(jdQueryOneKeySignBanksVo);
    }

    /**
     * 京东一键签约 - 一键签约申请
     */
    @PostMapping(value = "/oneKeySignApply", produces = "application/json;charset=UTF-8")
    public ResultVO<JDOneKeySignApplyDto> oneKeySignApply(@RequestBody JDOneKeySignApplyVo jdOneKeySignApplyVo){
        return paymentOpenAccountApi.oneKeySignApply(jdOneKeySignApplyVo);
    }

    /**
     * 京东一键签约 - 签约结果查询
     */
    @PostMapping(value = "/queryOneKeySignResult", produces = "application/json;charset=UTF-8")
    public ResultVO<JDBankCardBindDto> queryOneKeySignResult(@RequestBody JDQueryOneKeySignResultVo jdQueryOneKeySignResultVo){
        jdQueryOneKeySignResultVo.setSync(true);
        return paymentOpenAccountApi.queryOneKeySignResult(jdQueryOneKeySignResultVo);
    }

    /**
     * 京东 - 渠道卡信息查询
     */
    @PostMapping(value = "/cardbin", produces = "application/json;charset=UTF-8")
    public ResultVO<JDCardBinDataDto> cardbin(@RequestBody JDCardBinRequestVo jdCardBinRequestVo){
        return paymentOpenAccountApi.cardbin(jdCardBinRequestVo);
    }

    /**
     * 京东 - 协议签约接口
     */
    @PostMapping(value = "/contractSign", produces = "application/json;charset=UTF-8")
    public ResultVO<JDContractSignDto> contractSign(@RequestBody JDContractSignVo jdContractSignVo){
        return paymentOpenAccountApi.contractSign(jdContractSignVo);
    }

    /**
     * 京东 - 协议确认接口
     */
    @PostMapping(value = "/contractSignConfirm", produces = "application/json;charset=UTF-8")
    public ResultVO contractSignConfirm(@RequestBody JDContractSignConfirmVo jdContractSignConfirmVo){
        return paymentOpenAccountApi.contractSignConfirm(jdContractSignConfirmVo);
    }

    /**
     * 京东 - 协议解约接口
     */
    @PostMapping(value = "/cancelContract", produces = "application/json;charset=UTF-8")
    public ResultVO cancelContract(@RequestBody JDCancelContractVo jdCancelContractVo){
        return paymentOpenAccountApi.cancelContract(jdCancelContractVo);
    }

    /**
     * 京东 - 协议支付接口
     */
    @PostMapping(value = "/saleByContractNo", produces = "application/json;charset=UTF-8")
    public ResultVO<JDSaleByContractResponse> saleByContractNo(@RequestBody JDSaleByContractRequest jdSaleByContractRequest) throws Exception {
        String logPrefix = "jdCardPay " + jdSaleByContractRequest.getOutTradeNo();
        JDSaleByContractResponse jdSaleByContractResponse = jdAdpaterService.saleByContractNo(logPrefix, jdSaleByContractRequest);
        return ResultVO.createSuccess(jdSaleByContractResponse);
    }

    /**
     * 京东 - 协议退款接口
     */
    @PostMapping(value = "/tradeRefund", produces = "application/json;charset=UTF-8")
    public ResultVO<JDTradeRefundResponseVo> tradeRefund(@RequestBody JDTradeRefundRequestVo jdTradeRefundRequestVo) throws Exception {
        String logPrefix = "jdCardRefund " + jdTradeRefundRequestVo.getOutTradeNo();
        JDTradeRefundResponseVo jdTradeRefundResponseVo = jdAdpaterService.tradeRefund(logPrefix, jdTradeRefundRequestVo);
        return ResultVO.createSuccess(jdTradeRefundResponseVo);
    }

    /**
     * 京东 - 协议查询接口
     */
    @PostMapping(value = "/tradeQuery", produces = "application/json;charset=UTF-8")
    public ResultVO<JDTradeQueryResponseVo> tradeQuery(@RequestBody JDTradeQueryRequestVo jdTradeQueryRequestVo) throws Exception {
        String logPrefix = "jdCardQuery " + jdTradeQueryRequestVo.getOutTradeNo();
        JDTradeQueryResponseVo jdTradeQueryResponseVo = jdAdpaterService.tradeQuery(logPrefix, jdTradeQueryRequestVo);
        return ResultVO.createSuccess(jdTradeQueryResponseVo);
    }

    /**
     * 京东 - 查询客户绑卡列表
     */
    @PostMapping(value = "/bindCardList", produces = "application/json;charset=UTF-8")
    public ResultVO<List<JDBankCardBindDto>> bindCardList(@RequestBody JDBankCardBindVo jdBankCardBindVo) {
        return paymentOpenAccountApi.bindCardList(jdBankCardBindVo);
    }

    /**
     * 京东 - 清除绑卡信息
     */
    @GetMapping(value = "/clearMerchant", produces = "application/json;charset=UTF-8")
    public ResultVO clearMerchant(@RequestParam(value = "merchantNo") String merchantNo, @RequestParam(value = "bindAccount") String bindAccount) {
        return paymentOpenAccountApi.clearBindCards(merchantNo, bindAccount);
    }

    /**
     * 同步退款状态
     */
    @GetMapping(value = "/syncRefund", produces = "application/json;charset=UTF-8")
    public ResultVO syncRefund(@RequestParam(value = "channel") String channel) {
        syncRefundStatusJob.syncStatus(RefundOrder.STATUS_IN, channel);
        return ResultVO.createSuccess();
    }

}
